****Curtis RESTAT
**Curtis RESTAT
**Change to directory where data and code were downloaded
**cd 
set matsize 1500
clear all
set more off

use cbp_st_tableO7,clear

drop if enint==0 /*keeps only MFTG Industries*/
drop if fipstate=="11" /*Drops DC*/

do dataclean_tableO7.do

gen empweight2=emp if year==1998
bysort stateind:carryforward empweight2,replace

so stateind year
egen empweight3t=mean(emp),by(stateind post)
replace empweight3t=. if post==1
egen empweight3=max(empweight3t),by(stateind)

gen eaststr="east" if east==1
replace eaststr="west" if eaststr==""
gen eastind=eaststr+naics

drop yeardum*
tab year,gen(yeardum)


gen trend=year-1997
qui tab fipstate,gen(STdum)
foreach var of varlist ind1-ind21 {
qui gen trend`var'=`var'*trend
}

foreach x of varlist STdum1-STdum49 {
qui gen trend`x'=`x'*trend
}

/* Generate Seperate East/West Trends by Industry*/
forvalues i = 1(1)21 {
  qui gen eindtrend`i' = trendind`i'*east
 }
forvalues i = 1(1)21 {
  qui gen windtrend`i' = trendind`i'*((east-1)^2)
 }
tostring east,replace
gen yearindreg=yearind+"_"+east


qui tab yearind,gen(yearinddum)
qui tab STyear, gen(STyeardum)
qui tab stateind, gen(stateinddum)

/* Defines RHS variables to be included in every regression*/
mac def rhs_vars "postxint intxeast postxeast  "


/*match in enint from 1998-2009*/
destring east,replace
so naics
merge m:1 naics using enint9809clean_tableO7
gen enint_change=.
forvalues i = 1998(1)2009 {
  replace enint_change= enint`i' if year==`i'
 }
 
 ***The following code reports reg results where industries are weighted
 ***by each possible year. ennint_change allows int to vary year-to-year
foreach var of varlist enint1998-enint2009 enint_change {
	eststo clear
	  cap qui drop enint postxeastxint postxint intxeast
	  cap qui gen enint=`var'
	  cap qui gen postxeastxint=postxeast*enint
	  cap qui gen postxint=post*enint
	  cap qui gen intxeast=enint*east

	  /*Table O7*/
	  *Column 1
	  qui eststo:areg lemp postxeastxint postxint intxeast postxeast yeardum* [aweight=empweight2],abs(stateind) rob cluster(eastind)
	  *Column 2
	  qui eststo:areg lemp postxeastxint postxint intxeast postxeast yearinddum* trendSTdum* [aweight=empweight2],abs(stateind) rob cluster(eastind)
	  *Column 3
	  qui eststo:areg lemp postxeastxint postxint intxeast postxeast yearinddum* STyeardum* [aweight=empweight2],abs(stateind) rob cluster(eastind)
	  *Column 4
	  qui eststo:areg lemp postxeastxint postxint intxeast postxeast yeardum* windtrend* eindtrend*[aweight=empweight2],abs(stateind) rob cluster(eastind)
	  *Column 5
	  qui eststo:areg lemp postxeastxint postxint intxeast postxeast yearinddum* trendSTdum* windtrend* eindtrend*[aweight=empweight2],abs(stateind) rob cluster(eastind)
	  *Column 6
	  qui eststo:areg lemp postxeastxint postxint intxeast postxeast yearinddum* STyeardum* windtrend* eindtrend*[aweight=empweight2],abs(stateind) rob cluster(eastind)
	  outreg2 [est1 est2 est3 est4 est5 est6] using tableO7`var'.tex,keep(postxeastxint postxeast) nocons replace
}

